<template>
  <a-modal :maskClosable="false"
    :title='title'
    :width='800'
    :visible='visible'
    :confirmLoading='confirmLoading'
    @ok='handleOk'
    @cancel='handleCancel'
    cancelText='关闭'>

    <a-spin :spinning='confirmLoading'>
      <a-form-model ref='form' v-bind='layout' :model='model' :rules='validatorRules'>
        <a-form-model-item label='姓名'>
          {{ model.name }}
        </a-form-model-item>
        <a-form-model-item label='登录账号' required prop='loginName'>
          <a-input placeholder='请输入 登录账号' v-model='model.loginName' :disabled='disabled' />
        </a-form-model-item>
        <div class='ant-form-explain'>注：默认登录密码为123456。注如果已经存在开通帐号，密码不会进行改变</div>
<!--        <a-form-model-item label='密码' required prop='password'>-->
<!--          <a-input-password placeholder='请输入 密码' v-model='model.password' />-->
<!--        </a-form-model-item>-->
<!--        <a-form-model-item label='确认密码' required prop='surePassword'>-->
<!--          <a-input-password placeholder='请输入 确认密码' v-model='model.surePassword' />-->
<!--        </a-form-model-item>-->
      </a-form-model>
    </a-spin>
  </a-modal>
</template>

<script>
import { httpAction, putAction, getAction, postAction } from '@/api/manage'
import { queryall } from '@api/api'

export default {
  name: 'hHisNurseServiceCategoryModal',
  data() {
    return {
      title: '操作',
      visible: false,
      disabled: true,
      model: {
        userId: 0,
        roleId:'',
      },
      layout: {
        labelCol: { span: 4 },
        wrapperCol: { span: 14 }
      },
      confirmLoading: false,
      validatorRules: {
        roleId: [{ required: true, message: '请选择角色!' }],
        loginName: [{ required: true, message: '请输入登录账号!' }],
        password: [{ required: true, message: '请输入密码!' }],
        surePassword: [{ required: true, message: '请输入确认密码!' }]
      },
      url: {
        get: '/his/account/role/get',
        add: '/his/account/role',
        edit: '/his/common/category'
      },
      rolesOptions: []
    }
  },
  created() {
    this.modelDefault = JSON.parse(JSON.stringify(this.model))
    this.initRoleList()
  },
  methods: {
    //初始化角色字典
    initRoleList() {
      queryall().then((res) => {
        if (res.success) {
          this.rolesOptions = res.result;
          console.log('this.rolesOptions: ', this.rolesOptions)
        }
      })
    },
    edit(record) {
      this.model.name = record.name
      this.model.loginName = record.mobilePhone
      this.model.roleId = ''
      this.model.userId = record.userId
      this.model.source=record.source
      this.visible = true
      const that=this;
      postAction(this.url.get, { source: '1', userId: record.userId }).then((res) => {
        if (res.success && res.result.roleId!='') {
           that.model.roleId=res.result.roleId;
        }
      })
    },
    close() {
      this.$refs.form.clearValidate()
      this.$emit('close')
      this.visible = false
    },
    handleOk() {
      const that = this
      // 触发表单验证
      this.$refs.form.validate(valid => {
        if (valid) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          httpAction(httpurl, this.model, method).then((res) => {
            if (res.success) {
              that.$message.success(res.message)
              that.$emit('ok')
              that.close()
            } else {
              that.$message.warning(res.message)
            }
          }).finally(() => {
            that.confirmLoading = false
          })
        } else {
          return false
        }
      })
    },
    handleCancel() {
      this.close()
    }


  }
}
</script>

<style scoped>

</style>